package org.lex.swing.widgets;

import java.util.Properties;

import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.TableColumn;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;

import org.lex.swing.Swings;
import org.lex.swing.table.TableHelper;


public class SystemPropertiesTable extends JTable {
	public static void main(String[] args) {
		Swings.show(new JScrollPane(new SystemPropertiesTable()), false);
	}

	public SystemPropertiesTable() {
		super(createTableModel());
		this.setRowSorter(new TableRowSorter<TableModel>(this.getModel()));
		this.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
		TableColumn column = this.getColumnModel().getColumn(0);
		column.setPreferredWidth(180);
		column = this.getColumnModel().getColumn(1);
		column.setPreferredWidth(375);
	}

	private static TableModel createTableModel() {
		Properties prop = System.getProperties();
		String sep = prop.getProperty("line.separator");
		StringBuilder buf = new StringBuilder(sep.length());
		for (int i = 0; i < sep.length(); i++) {
			char ch = sep.charAt(i);
			switch (ch) {
			case '\n':
				buf.append("\\n");
				break;
			case '\r':
				buf.append("\\r");
				break;
			}
		}
		prop.setProperty("line.separator", buf.toString());
		return TableHelper.createModel(prop);
	}
}
